/*
 * Copyright 2001-2008 Software Engineering Center Chinese Academy of Sciences.
 * All rights reserved. SEC PROPRIETARY/CONFIDENTIAL. Use is subject to license
 * terms.
 */
package cn.ac.sec.framework.db;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

/**
 * 数据库连接管理工具 2.0版本改为了数据库连接池管理连接，可以直接获取DataSource
 * 
 * @author sec
 * @version 2.0
 */
public class ConnectionManager {
    
    // private final ComboPooledDataSource dataSource;
    /**
     * dataSource
     */
    private final BasicDataSource          dataSource;
    /**
     * INSTANCE
     */
    private static final ConnectionManager INSTANCE = new ConnectionManager();
    
    /**
     * @return
     */
    public static ConnectionManager getInstance() {
        return INSTANCE;
    }
    
    /**
     * 
     */
    private ConnectionManager() {
        DatabaseSettings settings = DatabaseSettings.getInstance();
        this.dataSource = new BasicDataSource();
        this.dataSource.setDriverClassName(settings.getJdbcDriver());
        this.dataSource.setUrl(settings.getJdbcUrl());
        this.dataSource.setUsername(settings.getJdbcUsername());
        this.dataSource.setPassword(settings.getJdbcPassword());
    }
    
    /**
     * 获取一个数据库连接
     * 
     * @return 数据库连接
     * @throws SQLException
     */
    public Connection getConnection() throws SQLException {
        return this.dataSource.getConnection();
    }
    
    /**
     * 获取DataSource
     * 
     * @return
     * @throws SQLException
     */
    public DataSource getDataSource(){
        return this.dataSource;
    }
    
    /**
     * @throws SQLException
     */
    public void close() throws SQLException {
        this.dataSource.close();
    }
}
